home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part1 / 4831 < prev    next >
Encoding:
Text File  |  1996-08-05  |  1.6 KB  |  55 lines

  1. Path: fontina.cs.wisc.edu!flisakow
  2. From: flisakow@fontina.cs.wisc.edu (Shaun Flisakowski)
  3. Newsgroups: comp.lang.c
  4. Subject: Re: ???Recursive Function needed for string printing
  5. Date: 7 Feb 1996 09:42:52 GMT
  6. Organization: U of Wisconsin CS Dept
  7. Message-ID: <4f9s6s$bb4@spool.cs.wisc.edu>
  8. References: <4f44eo$74u@upsidedown.MTS.Net>
  9. NNTP-Posting-Host: fontina.cs.wisc.edu
  10.  
  11. In article <4f44eo$74u@upsidedown.MTS.Net>, George <bwilliam@MTS.Net> wrote:
  12. >How do I write a recursive function to print a string  using only
  13. >printf and %c to print it out.
  14. >
  15. >George at bwilliam@MTS.Net
  16.  
  17.     Hope you don't mind if I combine a few threads, this is a pretty
  18.     busy group, you know.
  19.  
  20.     The following prints a string, given as the 1st command line argument
  21.     to the program, using printf with only %c.
  22.     
  23.     This is also a valid answer to the "can I call main()" thread,
  24.     as well as a hint for the "how do I stop recursing" thread,
  25.     (the answer of course, is to stop when the string is printed).
  26.  
  27.  
  28. #include <stdio.h>
  29. #include <string.h>
  30.  
  31. int main(int ac, char **av) {
  32.  
  33.     if ( ac = (ac > 0) ? (-strlen(av[1])) : ac+1)
  34.         main(ac,av);
  35.  
  36.     printf("%c", *(av[1]-ac-1) );
  37.  
  38.     if (!strlen(av[1]-ac))
  39.         printf("%c", '\n' );
  40.  
  41.     return(0);
  42. }
  43.  
  44.     Good luck with your class, and please feel free to send me huge
  45.     gobs of money.
  46.    
  47.     PS: In the future, try cross-posting, I've read these message
  48.     3 times already.  Also, it is not appropriate for comp.programming.
  49.  
  50. -- 
  51.     Shaun        flisakow@cs.wisc.edu
  52.  
  53.    "In your heart you know its flat."
  54.                            -Flat Earth Society
  55.